package com.steven.ourcontest.chat.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.base.BaseViewBinder
import com.steven.ourcontest.me.widget.GlideRoundTransform
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.team.bean.TeamInfo

class TeamViewBinder(
    private val context: Context,
    private val applyClick: ((teamId: Long) -> Unit)
) : BaseViewBinder<TeamInfo, TeamViewHolder>() {
    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): TeamViewHolder {
        val mInflater = inflater.inflate(R.layout.item_team_list, parent, false)
        return TeamViewHolder(mInflater)
    }

    override fun onBindViewHolder(itemHolder: TeamViewHolder, itemData: TeamInfo) {
        itemHolder.apply {
            tv_team_name?.text = itemData.teamName
            when (itemData.status) {
                0 -> {
                    tv_status?.isVisible = true
                    tv_status?.text = "招募状态:关闭"
                    tv_apply?.isVisible = false
                    tv_match?.isVisible = false
                }
                1 -> {
                    tv_status?.isVisible = true
                    tv_status?.text = "招募状态:开启"
                    tv_apply?.isVisible = true
                    tv_match?.isVisible = true
                }
                else -> {
                    tv_status?.isVisible = false
                    tv_apply?.isVisible = false
                    tv_match?.isVisible = false
                }
            }
            tv_match?.text = "面向竞赛:${itemData.matchName}"
            tv_member?.text = "已有成员:${itemData.memberNum}人"
            tv_apply?.setOnClickListener {
                applyClick.invoke(itemData.id)
            }
            itemView.setOnClickListener {
                if (itemData.status == 1) {
                    AppRouter.openTeamDetailActivity(context, itemData.id)
                } else {
                    Toast.makeText(context, "该队伍尚未招募", Toast.LENGTH_SHORT).show()
                }
            }
            tv_leader_name?.text = itemData.leaderName
            if (iv_icon != null) {
                Glide.with(context)
                    .load(NetConstants.ICON_BASE_URL + itemData.leaderIconUrl)
                    .error(R.drawable.ic_avatar)
                    .apply(RequestOptions.bitmapTransform(GlideRoundTransform(context, 10)))
                    .into(iv_icon)
            }
        }
    }
}

class TeamViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val iv_icon: ImageView? = itemView.findViewById(R.id.iv_icon)
    val tv_team_name: TextView? = itemView.findViewById(R.id.tv_team_name)
    val tv_status: TextView? = itemView.findViewById(R.id.tv_status)
    val tv_apply: TextView? = itemView.findViewById(R.id.tv_apply)
    val tv_match: TextView? = itemView.findViewById(R.id.tv_match)
    val tv_member: TextView? = itemView.findViewById(R.id.tv_member)
    val tv_leader_name: TextView? = itemView.findViewById(R.id.tv_leader_name)
}

